package com.fengchao.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发邮件工具
 * Created by wangnian on 2016/7/7.
 */
public class MailTools {

    private static Logger logger = LoggerFactory.getLogger(MailTools.class);


    public static boolean SendMail(String sendTo, String title, String text) {
        new Thread() {
            public void run() {
                try {
                    String sendUserName = "message@letswarm.com";
                    String sendPassword = "Fengqi2016";
                    Properties properties = new Properties();
                    properties.setProperty("mail.smtp.auth", "true");//服务器需要认证
                    properties.setProperty("mail.transport.protocol", "smtp");//声明发送邮件使用的端口
                    Session session = Session.getInstance(properties);
                    session.setDebug(false);//同意在当前线程的控制台打印与服务器对话信息
                    Message message = new MimeMessage(session);//构建发送的信息
                    message.setText(text);//信息内容
                    message.setSubject(title);
                    message.setFrom(new InternetAddress("message@letswarm.com"));//发件人
                    Transport transport = session.getTransport();
                    transport.connect("smtp.exmail.qq.com", 25, sendUserName, sendPassword);//连接发件人使用发件的服务器
                    transport.sendMessage(message, new Address[]{new InternetAddress(sendTo)});//接受邮件
                    transport.close();
                } catch (Exception ex) {
                    logger.warn("发送邮件消息失败=exception:{}", ex);
                }
            }
        }.start();
        return true;
    }
}